﻿using UnityEngine;
using System.Collections;

public class SupplyTruckObj : MonoBehaviour {

    public bool m_bShopingEnd;
    public TRUCK_EVENT m_EventId;

    bool m_bParked;

    float m_fSpeed;
    float m_fAccel;

    bool m_bTrumbleUp;

    bool m_bBrakeOnce;

    GameObject EventTimer;
    GameObject ShopBtn;

    Vector3 m_VecStop;
    Vector3 m_VecGoal;

	// Use this for initialization
	void Start () {
        m_bShopingEnd = false;

        EventTimer = Resources.Load("Prefaps/UI/StaticUI/Event/EventTimer") as GameObject;
        ShopBtn = Resources.Load("Prefaps/UI/Right/Shop/ShopBtn") as GameObject;

        transform.position = GameObject.Find("SupplyTruckRoute").transform.FindChild("Start").position;
        m_VecStop = GameObject.Find("SupplyTruckRoute").transform.FindChild("Stop").position;
        m_VecGoal = GameObject.Find("SupplyTruckRoute").transform.FindChild("Goal").position;

        switch (m_EventId)
        {
            case TRUCK_EVENT.REINFORCE:
                GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Sprite/Prop/truck/ReinforceTruck1");
                break;

            case TRUCK_EVENT.AMMO:
                GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Sprite/Prop/truck/AmmoTruck");
                break;

            case TRUCK_EVENT.SUPPLY:
                GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("Sprite/Prop/truck/WpnTruck");
                break;
        }

        m_fSpeed = 0.75f;
        m_fAccel = 0.01f;

        GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Truck_Move"));
	}
	
	// Update is called once per frame
	void Update () {

        if (transform.localPosition.x < m_VecGoal.x + 3.0f)
        {
            GetComponent<AudioSource>().volume -= 0.01f;
        }


        if (transform.localPosition.x < m_VecGoal.x)
        {
            GameObject.Find("TruckEvent(Clone)").GetComponent<TruckEvent>().bParking = false;
            Destroy(gameObject);
        }

        if (!m_bParked)
        {
            transform.localPosition = new Vector3(transform.localPosition.x - (m_fSpeed * Time.deltaTime), transform.localPosition.y);

            if (transform.localPosition.x < m_VecStop.x + 1.5f)
            {
                if (!m_bBrakeOnce)
                {
                    GetComponent<AudioSource>().Stop();
                    GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Truck_Brake"));
                    m_bBrakeOnce = true;
                }
            }

            if (transform.localPosition.x < m_VecStop.x + 1.25f)
            {
                if (m_fSpeed > 0)
                {
                    m_fSpeed -= m_fAccel * Time.deltaTime;
                    m_fAccel += 0.3f * Time.deltaTime;
                    if (m_fSpeed < 0.01f)
                        Trumble();
                }
                else
                {
                    m_fAccel = 0.01f;
                    m_fSpeed = 0;
                    m_bParked = true;

                    (Instantiate(EventTimer) as GameObject).transform.parent = GameObject.Find("EventMgr").transform;
                    (Instantiate(ShopBtn) as GameObject).transform.parent = GameObject.Find("RightBtn").transform;
                    GameObject.Find("ShopBtn(Clone)").GetComponent<ShopBtn>().EventID = this.m_EventId;

                    GetComponent<AudioSource>().loop = true;
                    GetComponent<AudioSource>().Play();
                }
            }
        }
        else
        {
            if (m_bShopingEnd)
            {

                if (m_bBrakeOnce)
                {
                    GetComponent<AudioSource>().Stop();
                    GetComponent<AudioSource>().PlayOneShot(Resources.Load<AudioClip>("Audio/Truck_Move"));
                    m_bBrakeOnce = false;
                }

                transform.localPosition = new Vector3(transform.localPosition.x - (m_fSpeed * Time.deltaTime), transform.localPosition.y);

                if (m_fSpeed < 2.0f)
                {
                    m_fSpeed += m_fAccel * Time.deltaTime;
                    m_fAccel += 0.5f * Time.deltaTime;
                    if (m_fSpeed < 0.05f)
                        Trumble();
                }
                else
                {
                    m_fSpeed = 2.0f;
                }
            }
        }
	}

    void Trumble()
    {
        if (!m_bTrumbleUp)
        {
            if (transform.localPosition.y > m_VecStop.y - 0.0002f)
                transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y - (0.4f * Time.deltaTime));
            else
            {
                transform.localPosition = new Vector3(transform.localPosition.x, m_VecStop.y - 0.0002f);
                m_bTrumbleUp = true;
            }
        }
        else
        {
            if (transform.localPosition.y < m_VecStop.y + 0.0002f)
                transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + (0.4f * Time.deltaTime));
            else
            {
                transform.localPosition = new Vector3(transform.localPosition.x, m_VecStop.y + 0.0002f);
                m_bTrumbleUp = false;
            }
        }
        
    }
}
